﻿using System.Collections.Generic;
using Engine.Math;
using Microsoft.Xna.Framework;
using XNAnimation;

namespace Engine.Animations
{ 
    public class Animation
    {
        public Animation()
        {
            CurrentClip = IdleClipName;
        }

        #region Consts

        public string IdleClipName = "Idle";
        public string RunClipName = "Run";

        #endregion


        #region Properties

        public string CurrentClip { get; set; }
        public SkinnedModel SkinnedModel { get; private set; }
        public Vector3 Translation { get; set; }
        public float RotationY { get; set; }
        public Vector3 Scale { get; set; }
        public Matrix WorldMatrix { get; private set; }
        public BoundingBox InitialBoundingBox { get; private set; }

        #endregion

        #region Public Methods

        public void SetDirection(Vector3 direction)
        {        
            double angle = MathHelperEx.GetAngleBetweenVectors(new Vector2(direction.X, direction.Z),
                                                                             new Vector2(0f, 1f));
            RotationY = (float)-angle + MathHelper.Pi;
        }
        public void SetDirectionToPoint(Vector3 point)
        {
            Vector3 direction = point - Translation;
            direction.Normalize();
            double angle = MathHelperEx.GetAngleBetweenVectors(new Vector2(direction.X, direction.Z),
                                                                             new Vector2(0f, 1f));

            RotationY = (float) -angle + MathHelper.Pi;
        }
        virtual public void Load(Game game, string modelFileName)
        {
            SkinnedModel = game.Content.Load<SkinnedModel>(modelFileName);
            Dictionary<string, object> tag = (Dictionary<string, object>)SkinnedModel.Model.Tag;
            InitialBoundingBox = (BoundingBox)tag["BoundingBox"];
        }
        virtual public Matrix Update(GameTime time)
        {
            Matrix worldMatrix =Matrix.CreateRotationY(RotationY)* Matrix.CreateScale(Scale)*Matrix.CreateTranslation(Translation);
            WorldMatrix = worldMatrix;
            return worldMatrix;
        }

        #endregion

        #region Private Methods

        #endregion
    }
}
